# coding:utf-8
# Author : hiicy redldw
# Date : 2019/04/12
''
import numpy

"""
分析数据
数据清洗
"""
from matplotlib import pyplot as plt
import pandas as pd
import xgboost as xgb
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split,cross_validate
from sklearn.model_selection import GridSearchCV,StratifiedKFold
from xgboost.plotting import plot_importance,plot_tree
def preData(filepath):
    data = pd.read_csv(filepath,header=0)
    data=data.drop(['Name','Ticket','Embarked'],axis=1)
    data['Age'] = data['Age'].fillna(data['Age'].median())
    data['child'] = data['Age'].apply(lambda x:1 if x<15 else 0)
    # data[data['Sex'] == 'male']这是按行过滤选择数据
    # data.loc[data['Sex']=='male','Sex'] = 0
    # data.sex[data['Sex']=="female"] = 1
    # map 针对一列
    data['Sex']=data['Sex'].map(lambda x:0 if x=="male" else 1)
    data['Cabin']=data['Cabin'].map(lambda x:0 if pd.isna(x) else 1)

    label = data.pop('Survived')
    print(len(data))
    return data.values,label.values

def buildModel():
    data,label = preData(r'F:\Resources\Dataset\Titanic\train.csv')
    xtrain,xtest,ytrain,ytest= train_test_split(data,
                                                 label,
                                                 test_size=0.2,
                                                 random_state=22)
    dtrain = xgb.DMatrix(xtrain,ytrain)
    dtest = xgb.DMatrix(xtest,ytest)
    params={"objective": "binary:logistic", 'colsample_bytree': 0.9, 'learning_rate': 0.0001,
            'max_depth': 15, 'alpha': 0.7,"min_child_weight":0.1}
    model:xgb.Booster = xgb.train(params,dtrain,num_boost_round=10)
    ypred:numpy.ndarray = model.predict(dtest)
    # print(ypred.round())
    ytrue = dtest.get_label()
    ytrue.dtype=numpy.float32

    # 网格搜索
    paramst={
        'max_depth':range(2,30,2)
    }
    gs = GridSearchCV(model,paramst,n_jobs=2,cv=5)
    gs.fit(xtrain,ytrain)

    train_accuracy = accuracy_score(ytrue,ypred.round())
    print('train Accuary:{:.2f}%'.format(train_accuracy*100.0))
    return model

def runTest():
    testFile = r'F:\Resources\Dataset\Titanic\test.csv'
    testData = pd.read_csv(testFile, header=0)
    testData = testData.drop(['Name', 'Ticket', 'Embarked'], axis=1)
    testData['Age'] = testData['Age'].fillna(testData['Age'].median())
    testData['child'] = testData['Age'].apply(lambda x:1 if x<15 else 0)

    testData['Sex'] = testData['Sex'].map(lambda x: 0 if x == "male" else 1)
    testData['Cabin'] = testData['Cabin'].map(lambda x: 0 if pd.isna(x) else 1)
    model = buildModel()
    dtest = xgb.DMatrix(testData.values)
    dpred = model.predict(dtest)
    print(type(dpred),dpred.round())
    dataFrame=pd.DataFrame(columns=['PassengerId','Survived'],index=None)
    dataFrame['PassengerId'] = testData['PassengerId']
    dataFrame['Survived'] = pd.Series([int(i) for i in dpred.round()])
    dataFrame.to_csv(r'F:\Resources\Dataset\Titanic\result.csv',index=None)
buildModel()
# plt.show()

#######  others code ##############
import numpy as np
import pandas as pd
import re as re

train = pd.read_csv('../input/train.csv', header = 0, dtype={'Age': np.float64})
test  = pd.read_csv('../input/test.csv' , header = 0, dtype={'Age': np.float64})
full_data = [train, test]

print (train.info())
print (train[['Pclass', 'Survived']].groupby(['Pclass'], as_index=False).mean())



